<template>
  <view class="task-create">
    <uni-forms ref="form" :modelValue="formData" :rules="rules">
      <uni-forms-item label="维护类型" name="maintenanceType">
        <uni-data-select
          v-model="formData.maintenanceType"
          :localdata="maintenanceTypes"
          placeholder="请选择维护类型"
        />
      </uni-forms-item>

      <uni-forms-item label="电梯" name="elevatorId">
        <uni-data-select
          v-model="formData.elevatorId"
          :localdata="elevatorOptions"
          placeholder="请选择电梯"
        />
      </uni-forms-item>

      <uni-forms-item label="维护人员" name="maintainerId">
        <uni-data-select
          v-model="formData.maintainerId"
          :localdata="maintainerOptions"
          placeholder="请选择维护人员"
        />
      </uni-forms-item>

      <uni-forms-item label="开始时间" name="startTime">
        <uni-datetime-picker
          v-model="formData.startTime"
          type="datetime"
          placeholder="请选择开始时间"
        />
      </uni-forms-item>

      <uni-forms-item label="预计时长(小时)" name="estimatedDuration">
        <uni-easyinput
          v-model="formData.estimatedDuration"
          type="number"
          placeholder="请输入预计时长"
        />
      </uni-forms-item>

      <uni-forms-item label="优先级" name="priority">
        <uni-data-select
          v-model="formData.priority"
          :localdata="priorityOptions"
          placeholder="请选择优先级"
        />
      </uni-forms-item>

      <uni-forms-item label="描述" name="description">
        <uni-easyinput
          v-model="formData.description"
          type="textarea"
          placeholder="请输入任务描述"
        />
      </uni-forms-item>
    </uni-forms>

    <button class="submit-btn" @click="handleSubmit">提交</button>
  </view>
</template>

<script>
import { request } from '@/utils/request'

export default {
  data() {
    return {
      formData: {
        maintenanceType: '',
        elevatorId: '',
        maintainerId: '',
        startTime: '',
        estimatedDuration: '',
        priority: '',
        description: ''
      },
      rules: {
        maintenanceType: {
          rules: [{ required: true, errorMessage: '请选择维护类型' }]
        },
        elevatorId: {
          rules: [{ required: true, errorMessage: '请选择电梯' }]
        },
        maintainerId: {
          rules: [{ required: true, errorMessage: '请选择维护人员' }]
        },
        startTime: {
          rules: [{ required: true, errorMessage: '请选择开始时间' }]
        },
        estimatedDuration: {
          rules: [{ required: true, errorMessage: '请输入预计时长' }]
        },
        priority: {
          rules: [{ required: true, errorMessage: '请选择优先级' }]
        }
      },
      maintenanceTypes: [
        { value: 'ROUTINE', text: '例行维护' },
        { value: 'REPAIR', text: '故障维修' },
        { value: 'INSPECTION', text: '安全检查' }
      ],
      elevatorOptions: [],
      maintainerOptions: [],
      priorityOptions: [
        { value: 1, text: '低' },
        { value: 2, text: '中' },
        { value: 3, text: '高' }
      ]
    }
  },

  onShow() {
    this.loadElevators()
    this.loadMaintainers()
  },

  methods: {
    async loadElevators() {
      try {
        const elevators = await request({
          url: '/elevators'
        })
        this.elevatorOptions = elevators.map(elevator => ({
          value: elevator.id,
          text: `${elevator.building} - ${elevator.location}`
        }))
      } catch (err) {
        console.error('Failed to load elevators:', err)
      }
    },

    async loadMaintainers() {
      try {
        const users = await request({
          url: '/users',
          data: {
            role: 'MAINTAINER'
          }
        })
        this.maintainerOptions = users.map(user => ({
          value: user.id,
          text: user.username
        }))
      } catch (err) {
        console.error('Failed to load maintainers:', err)
      }
    },

    async handleSubmit() {
      try {
        await this.$refs.form.validate()
        
        await request({
          url: '/tasks',
          method: 'POST',
          data: this.formData
        })

        uni.showToast({
          title: '创建成功',
          icon: 'success'
        })

        uni.navigateBack()
      } catch (err) {
        if (err.errMsg) {
          uni.showToast({
            title: err.errMsg,
            icon: 'none'
          })
        } else {
          uni.showToast({
            title: '创建失败',
            icon: 'none'
          })
        }
      }
    }
  }
}
</script>

<style>
.task-create {
  padding: 15px;
}

.submit-btn {
  margin-top: 20px;
  background-color: #007AFF;
  color: #fff;
}
</style> 